package com.dragon.client.controller.contract;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.contract.IRejectedService;
import com.dragon.repository.domain.dto.ProduceDTO;
import com.dragon.repository.domain.dto.RejectedDTO;
import com.dragon.repository.domain.dto.RejectedProductDTO;
import com.dragon.repository.domain.vo.RejectedProductVO;
import com.dragon.repository.domain.vo.RejectedVO;
import com.dragon.repository.entity.Produce;
import com.dragon.repository.entity.Rejected;
import com.dragon.repository.entity.RejectedProduct;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 退货表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-12-05
 */
@RestController
@RequestMapping("rejected")
@Api(tags = "退货")
public class RejectedController {

    @Autowired
    IRejectedService service;

    @GetMapping("list")
    @ApiOperation(value = "退货列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",required = false,type = "string",value = "合同名称"),
            @ApiImplicitParam(name = "status",required = false,type = "int",value = "退货状态"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int")
    })
    public ResponseData<List<RejectedDTO>> rejectedList(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer status,
            @RequestParam Integer page,
            @RequestParam Integer limit) {
        Page<Rejected> rejectedPage = service.rejectedList(name,status,page,limit);
        return ResponseData.success(RejectedDTO.convertList(rejectedPage.getRecords()),rejectedPage.getTotal());
    }


    @GetMapping("detail")
    @ApiOperation(value = "获取退货详情")
    public ResponseData selectDetailById(@RequestParam @ApiParam(name = "id",required = true,type = "int",value = "主id") Integer id){
        Rejected rejected = service.selectDetailById(id);
        RejectedDTO rejectedDTO = RejectedDTO.convert(rejected);
        rejectedDTO.setRejectedProductDTOS(RejectedProductDTO.convertList(rejected.getRejectedProducts()));
        return ResponseData.success(rejectedDTO);
    }

    @PostMapping("add")
    @ApiOperation(value = "添加退货")
    @AddPage
    public ResponseData rejectedOperate(@RequestBody @Validated(Insert.class)RejectedVO vo){
        Rejected rejected = RejectedVO.convert(vo);
        rejected.setRejectedProducts(RejectedProductVO.convertList(vo.getRejectedProductVO()));
        return ResponseData.success(service.rejectedOperate(rejected));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改退货")
    public ResponseData rejectedUpdate(@RequestBody @Validated(Update.class)RejectedVO vo){
        Rejected rejected = RejectedVO.convert(vo);
        rejected.setRejectedProducts(RejectedProductVO.convertList(vo.getRejectedProductVO()));
        service.rejectedUpdate(rejected);
        return ResponseData.success();
    }

    @DeleteMapping("cancel")
    @ApiOperation(value = "取消退货")
    public ResponseData rejectedDelete(@ApiParam(name = "id", value = "id", required = true, type = "int")
                                       @RequestParam Integer id){
        service.rejectedDelete(id);
        return ResponseData.success();
    }

    @GetMapping("getIsDeliverList")
    @ApiOperation(value = "获取已有送货的生产单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search",value = "合同编码或者客户名称",required = false,type = "string"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,type = "int"),
            @ApiImplicitParam(name = "limit",value = "条数",required = true,type = "int"),
    })
    public ResponseData<List<ProduceDTO>> getIsDeliverList(
            @RequestParam(required = false) String search,
            @RequestParam Integer page,
            @RequestParam Integer limit
    ){
        Page<Produce> deliverProduce = service.isDeliverProduce(search,page,limit);
        return ResponseData.success(ProduceDTO.convertList(deliverProduce.getRecords()),deliverProduce.getTotal());
    }

    @GetMapping("getRejectedProductList")
    @ApiOperation(value = "获取退货产品列表")
    public ResponseData<List<RejectedProductDTO>> getRpList(
            @ApiParam(name = "rejectedId", value = "退货的主表id", required = true, type = "int")
            @RequestParam Integer rejectedId){
        List<RejectedProduct> rpList = service.getRpList(rejectedId);
        return ResponseData.success(RejectedProductDTO.convertList(rpList));
    }

}
